namespace Sharpmark.PerfectLife.ColorfulPhotos.TextualPhoto.SubFunctionForm
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using Sharpmark.PerfectLife.Core.Picture;
    using Sharpmark.PerfectLife.Core.Picture.Utilities.ImageToText;

    public partial class FrmPhotoText : Form
    {
        private PhotoText _photo;				// 读入的照片
        private bool _isKeepScale = true;		// 是否保持长宽比不变

        public FrmPhotoText()
        {
            InitializeComponent();

            _photo = new PhotoText(new Photo(
                (int)nudCharsWidth.Value, (int)nudCharsHeight.Value));

            _isKeepScale = ckbIsKeepScale.Checked;

            txtGeneratedText.Font = new System.Drawing.Font("新宋体", 9);

            SetControlsEnabled(false);
        }

        private void btnBrowerFile_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == this.ofdOpenImageFile.ShowDialog())
            {
                this.txtFileName.Text = this.ofdOpenImageFile.FileName;
            }
        }

        private void nudCharsWidth_ValueChanged(object sender, EventArgs e)
        {
            _photo.CharsWidth = (int)nudCharsWidth.Value;
            if (_isKeepScale)
                nudCharsHeight.Value = ScaleHeightByWidth(nudCharsWidth.Value);
        }

        private void nudCharsHeight_ValueChanged(object sender, EventArgs e)
        {
            _photo.CharsHeight = (int)nudCharsHeight.Value;
            if (_isKeepScale)
                nudCharsWidth.Value = ScaleWidthByHeight(nudCharsHeight.Value);
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            this.txtGeneratedText.Text = _photo.GenerateText();

            // 文件名的确定放在图像生成之后是必须的，不能等到点击保存按钮的时候才做这件事情。
            // 设置使得对话框初始状态时，默认文件名跟原文件的，不含扩展名的文件名相同
            System.IO.FileInfo fi = new System.IO.FileInfo(txtFileName.Text);
            sfdSaveText.FileName = (fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length));	// 去掉文件路径、后缀名的文件
        }

        private void ckbIsKeepScale_CheckedChanged(object sender, EventArgs e)
        {
            _isKeepScale = ckbIsKeepScale.Checked;
            // 激活改变事件，使得长宽比自动调节
            nudCharsWidth_ValueChanged(nudCharsWidth, new EventArgs());
        }

        private void rbnGrayLevelXX_CheckChanged(object sender, EventArgs e)
        {
            _photo.GrayLevel = Int32.Parse(((RadioButton)sender).Tag.ToString());

            ControlsValidate();
        }

        private void ckbAntiColor_CheckedChanged(object sender, EventArgs e)
        {
            _photo.IsAntiColor = ckbAntiColor.Checked;
        }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(this.txtFileName.Text))
            {
                _photo.Photo = new Photo(this.txtFileName.Text);

                // 当前文件可用，则将控件设为可用
                SetControlsEnabled(true);

                // 计算图像值
                nudCharsWidth.Value = _photo.Photo.Width / 3;
                if (!_isKeepScale)
                    nudCharsHeight.Value = _photo.Photo.Height / 5;
            }
            else
            {
                // 当前文件不可用，则将控件设为不可用
                SetControlsEnabled(false);
            }

            ControlsValidate();
        }

        private void SetControlsEnabled(bool enabled)
        {
            gpbCharsSize.Enabled = enabled;
            gpbGrayLevel.Enabled = enabled;
            btnThumbnailView.Enabled = enabled;
            // btnSave什么时候都可用。
            // btnSave.Enabled = enabled;
        }

        private void ControlsValidate()
        {
            if (File.Exists(txtFileName.Text)
                && (rbnGrayLevel2.Checked || rbnGrayLevel8.Checked || rbnGrayLevel94.Checked))
            {
                btnGenerate.Enabled = true;
            }
            else
            {
                btnGenerate.Enabled = false;
            }
        }

        private void btnThumbnailView_Click(object sender, EventArgs e)
        {
            (new FrmThumbnailView(txtFileName.Text)).Show();
        }

        private void textBox_SelectAll(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void numbericUpDown_SelectAll(object sender, EventArgs e)
        {
            NumericUpDown nup = (NumericUpDown)sender;
            nup.Select(0, nup.Value.ToString().Length);
        }

        private decimal ScaleWidthByHeight(decimal h)
        {
            // 同比缩放长宽
            // *3 / 5 是因为大多数字体长宽比5:3，为了使图像显示比较正常
            return _photo.Photo.Width * h * 5 / _photo.Photo.Height / 3;
        }

        private decimal ScaleHeightByWidth(decimal w)
        {
            // 同比缩放长宽
            return _photo.Photo.Height * w * 3 / _photo.Photo.Width / 5;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (sfdSaveText.ShowDialog() == DialogResult.OK)
            {
                // 写入文件
                System.IO.StreamWriter sw = new System.IO.StreamWriter(
                    sfdSaveText.FileName, false);

                sw.Write(txtGeneratedText.Text);

                sw.Close();
            }
        }

        private void btnFontSetting_Click(object sender, EventArgs e)
        {
            fdFontSelect.ShowDialog();
            this.txtGeneratedText.Font = fdFontSelect.Font;
        }
    }
}